package bosque.unc;

import bosque.unc.exceptions.ManoplaException;

public class ControlVelocidadTelecopio extends Thread{
	
	private Telescopio telescopio;
	private Integer delayVelocidad = 100;
	private Integer paso = 1;
	private Integer velMin = 0;
	private Integer velMax = 0;
	private Boolean incrementarVel = true;  // true -> incrementa; false -> decrementa
	private Integer velocidad = 0;
	private Boolean detencion = false;
	
	public ControlVelocidadTelecopio(Telescopio tel, Integer vMin, Integer vMax, Integer delay, Boolean incV, Boolean det)
	{
		this.telescopio = tel;
		this.velMax = vMax;
		this.velMin = vMin;
		this.incrementarVel = incV;
		this.delayVelocidad = delay;
		this.detencion = det;
	}
	
	public void run()
	{
		if(telescopio.getAcelerando())
		{
			this.interrupt();
		}
		
		telescopio.setAcelerando(true);
		System.out.println("Nuevo Hilo ControlVelocidad ( "+ this.getId() +" ) - acelerar " + this.incrementarVel);
				
		if(this.incrementarVel)
		{
			this.velocidad = velMin;
			while(velocidad <= this.velMax && telescopio.getAcelerando())
			{
				try {
					this.telescopio.setearVelocidad(velocidad);
				} catch (ManoplaException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
				velocidad += paso;				
				try {
					Thread.sleep(this.delayVelocidad);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
		else
		{
			
			this.velocidad = this.telescopio.getVelMov();
			while(velocidad >= this.velMin && telescopio.getAcelerando())
			{
				//this.telescopio.setVelMov(velocidad);
				try {
					this.telescopio.setearVelocidad(velocidad);
				} catch (ManoplaException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
				
				velocidad -= paso;				
				try {
					Thread.sleep(this.delayVelocidad);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			
			
		}
		
		if(this.detencion)
		{
			this.telescopio.setAcelerando(false);
			this.telescopio.notificarTelescopioDetencion();
			//notifyAll();
		}
		this.telescopio.notificarFinAceleracion();
		System.out.println("Cerrando Hilo ControlVelocidad ( "+ this.getId() +" ) - acelerar " + this.incrementarVel);
	}
	

	
	
}
